;; The first four lines of this file were added by Dracula.
;; They tell DrScheme that this is a Dracula Modular ACL2 program.
;; Leave these lines unchanged so that DrScheme can properly load this file.
#reader(planet "reader.rkt" ("cce" "dracula.plt") "modular" "lang")
(require "Iavgperformance.lisp")
(require "Istockhelper.lisp")

(module Mavgperformance

(import Istockhelper)

(include-book "io-utilities" :dir :teachpacks)
  
#| -------------------------------------------|#

#| *** functions to compute average performance query ***
|#
;; gets daily performance of a specific ticker 
(defun daily-performance (ticker-date-data)
  (let* ((data (cdr ticker-date-data))
         (opening-price (first data))
         (closing-price (fourth data)))
    (/ (- closing-price opening-price) opening-price)))

;; gets sum of daily performances of a specific ticker over a range of dates
(defun sum-daily-performance-over-range-of-dates (accumulator data-over-range-of-dates)
  (if (consp data-over-range-of-dates)
      (sum-daily-performance-over-range-of-dates (+ accumulator
                                                   (daily-performance (car data-over-range-of-dates)))
                                                (cdr data-over-range-of-dates))
      accumulator))

(defun avg-performance-response (ticker start-date end-date ticker-list flat-data-avl)
  (let* ((ticker-data (get-ticker-data ticker ticker-list flat-data-avl))
         (data-over-range-of-dates (get-data-over-range-of-dates start-date end-date ticker-data))
         (sum-daily-performances (sum-daily-performance-over-range-of-dates 0 data-over-range-of-dates))
         (num-of-days (len data-over-range-of-dates)))
    (if (equal data-over-range-of-dates nil)
        (concatenate 'string
                     ticker
                     ","
                     (rat->str (* sum-daily-performances 100) 2)
                     "%")
        (concatenate 'string
                     ticker
                     ","
                     (rat->str (* (/ sum-daily-performances num-of-days) 100) 2)
                     "%"))))
       
;; computes average performance query
; output is a list of strings of average performance for each ticker (over a range of dates)
(defun avg-performance-query (start-date end-date query-tickers ticker-list flat-data-avl)
  (if (consp query-tickers)
      (cons (avg-performance-response (car query-tickers) start-date end-date ticker-list flat-data-avl)
            (avg-performance-query start-date
                                   end-date
                                   (cdr query-tickers)
                                   ticker-list
                                   flat-data-avl))
      nil))
#| -------------------------------------------|#


(export Iavgperformance))